const express = require('express');
const router = express.Router();
const mysql = require('../conn');

// 后台菜品查询
router.get('/inquire', (req, res) => {
    let obj = {}
    let sql = `select d.*,c.title from dish d left join catelist c on d.category_id = c.id;`
    mysql.query(sql, (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 20
        obj.desc = '查询成功'
        obj.data = result
        res.send(obj)
    })
})
// 菜品分类查询
router.get('/catelist', (req, res) => {
    let obj = {}
    let sql = `select * from catelist`
    mysql.query(sql, (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 20
        obj.desc = '查询成功'
        obj.data = result
        res.send(obj)
    })
})
// 查询单个菜品
router.post('/inquire_one', (req, res) => {
    let { id } = req.body
    let obj = {}
    let sql = `select * from dish where id=?`
    mysql.query(sql, [id], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 20
        obj.desc = '查询成功'
        obj.data = result
        res.send(obj)
    })
})
// 菜品模糊查询
router.get('/inquire_dish', (req, res) => {
    let search = req.query.search
    let obj = {}
    let sql = `select * from dish where name like ? or description like ? `
    mysql.query(sql, ['%' + search + '%', '%' + search + '%'], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 20
        obj.desc = '查询成功'
        obj.data = result
        res.send(obj)
    })
})
// 菜单分类查询
router.post('/list', (req, res) => {
    let { id } = req.body
    let obj = {}
    let sql = `select dish.id,dish.name,dish.price,dish.description,dish.image,dish.sales,dish.old_price from dish,catelist where dish.category_id = catelist.id and catelist.id = ?;`
    mysql.query(sql, [id], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 20
        obj.desc = '查询成功'
        obj.data = result
        res.send(obj)
    })
})


// 后台菜品添加
router.post('/add', (req, res) => {
    let { name, category_id, old_price, price, image, description, sales, net_content, quality, storage_conditions, production, status } = req.body
    let obj = {}
    let sql = `insert into dish (name,category_id,old_price,price,image, description, sales,net_content,quality,storage_conditions,production,status) value (?,?,?,?,?,?,?,?,?,?,?,?)`
    mysql.query(sql, [name, category_id, old_price, price, image, description, sales, net_content, quality, storage_conditions, production, status], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '添加成功'
        obj.data = result
        console.log('添加成功');
        res.send(obj)
    })
})
// 后台菜品修改 使用关键字时可以用`\ \`转译
router.post('/update', (req, res) => {
    let { id, name, category_id, old_price, price, image, description, sales, net_content, quality, storage_conditions, production, status } = req.body
    let obj = {}
    let sql = `update dish set name=?,category_id=?,old_price=?,price=?,image=?,description=?,sales=?,net_content=?,quality=?,storage_conditions=?,production=?,status=? where id=?`
    mysql.query(sql, [name, category_id, old_price, price, image, description, sales, net_content, quality, storage_conditions, production, status, id], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '修改成功'
        obj.data = result
        console.log('修改成功');
        res.send(obj)
    })
})
// 后台菜品删除
router.post('/delete', (req, res) => {
    let { ids } = req.body
    let obj = {}
    let sql = `delete from dish where id in (?)`
    mysql.query(sql, [ids], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '删除成功'
        obj.data = result
        console.log('删除成功');
        res.send(obj)
    })
})
// 后台套餐查询
router.get('/inquire_taocan', (req, res) => {
    let obj = {}
    let sql = `select s.*,c.title from setmeal s left join catelist1 c on s.category_id = c.id;`
    mysql.query(sql, (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 20
        obj.desc = '查询成功'
        obj.data = result
        res.send(obj)
    })
})
// 后台套餐分类查询
router.get('/catelist1', (req, res) => {
    let obj = {}
    let sql = `select * from catelist1`
    mysql.query(sql, (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 20
        obj.desc = '查询成功'
        obj.data = result
        res.send(obj)
    })
})


// 后台套餐添加
router.post('/add_taocan', (req, res) => {
    let { category_id, name, price,old_price, copies, status, description, image, details } = req.body
    let obj = {}
    let sql = `insert into setmeal (category_id,name,price,old_price,copies,status,description,image,details) value (?,?,?,?,?,?,?,?,?)`
    mysql.query(sql, [category_id, name, price, old_price,copies, status, description, image, details], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '添加成功'
        obj.data = result
        console.log('添加成功');
        res.send(obj)
    })
})
// 后台套餐修改 使用关键字时可以用`\ \`转译
router.post('/update_taocan', (req, res) => {
    let { id, category_id, name, price,old_price, copies, status, description, image, details } = req.body
    let obj = {}
    let sql = `update setmeal set category_id=?,name=?,price=?,old_price=?,copies=?,status=?,description=?,image=?,details=? where id=?`
    mysql.query(sql, [category_id, name, price,old_price, copies, status, description, image, details, id], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '修改成功'
        obj.data = result
        console.log('修改成功');
        res.send(obj)
    })
})
// 后台套餐删除
router.post('/delete_taocan', (req, res) => {
    let { ids } = req.body
    let obj = {}
    let sql = `delete from setmeal where id in (?)`
    mysql.query(sql, [ids], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '删除成功'
        obj.data = result
        console.log('删除成功');
        res.send(obj)
    })
})

// 后台登陆
router.post('/login', (req, res) => {
    let { username, password } = req.body
    console.log(req.body);
    let obj = {}
    let sql = `select * from master where username=? and password=?`
    mysql.query(sql, [username, password], (err, result) => {
        console.log(result.length);
        if (err) {
            return console.log(err);
        }
        if (result.length > 0) {
            obj.desc = '登陆成功'
        } else {
            obj.code = 404

            obj.desc = '登陆失败'
        }

        obj.data = result
        console.log(obj);
        res.send(obj)
    })
})

// 后台注册
router.post('/register', (req, res) => {
    let { username, password } = req.body
    console.log(req.body);
    let obj = {}
    let sql = `insert into master (username,password) value (?,?)`
    mysql.query(sql, [username, password], (err, result) => {
        console.log(result.length);
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '注册成功'
        obj.data = result
        console.log(obj);
        res.send(obj)
    })
})

// 查询订单信息
router.get('/inquire_order', (req, res) => {
    let obj = {}
    let sql = `select  o.*,a.address_book from orders o left join address a on o.address_book_id = a.id;`
    mysql.query(sql, (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '查询成功'
        obj.data = result
        console.log('查询成功');
        res.send(obj)
    })
})
// 添加订单信息
router.post('/add_order', (req, res) => {
    let { number, status, user_id, address_book_id, order_time, checkout_time, pay_method, amount, remark } = req.body
    let obj = {}
    let sql = `insert into orders  (number,status,user_id,address_book_id,order_time,checkout_time,pay_method,amount,remark) value (?,?,?,?,?,?,?,?,?)`
    mysql.query(sql, [number, status, user_id, address_book_id, order_time, checkout_time, pay_method, amount, remark], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '添加成功'
        obj.data = result
        console.log('添加成功');
        res.send(obj)
    })
})
// 删除订单信息
router.post('/delete_order', (req, res) => {
    let { ids } = req.body
    let obj = {}
    let sql = `delete from orders where id in (?)`
    mysql.query(sql, [ids], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '删除成功'
        obj.data = result
        console.log('删除成功');
        res.send(obj)
    })
})


// 修改订单信息
router.post('/update_order', (req, res) => {
    let { id, number, status, user_id, address_book_id, order_time, checkout_time, pay_method, amount, remark } = req.body
    if(pay_method == '微信') pay_method=1
    else pay_method=2
    let obj = {}
    let sql = `update orders set number=?,status=?,user_id=?,address_book_id=?,order_time=?,checkout_time=?,pay_method=?,amount=?,remark=? where id=?`
    mysql.query(sql, [number, status, user_id, address_book_id, order_time, checkout_time, pay_method, amount, remark, id], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '修改成功'
        obj.data = result
        console.log('修改成功');
        res.send(obj)
    })
})




// 查询地址信息
router.get('/inquire_address', (req, res) => {
    let obj = {}
    let sql = `select * from address`
    mysql.query(sql, (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '查询成功'
        obj.data = result
        console.log('查询成功');
        res.send(obj)
    })
})
// 添加地址信息
router.post('/add_address', (req, res) => {
    let { user_id, consignee, sex, phone, address_book, detail } = req.body
    let obj = {}
    let sql = `insert into address (user_id,consignee,sex,phone,address_book,detail) value (?,?,?,?,?,?)`
    mysql.query(sql, [user_id, consignee, sex, phone, address_book, detail], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '添加成功'
        obj.data = result
        console.log('添加成功');
        res.send(obj)
    })
})
// 删除地址信息
router.post('/delete_address', (req, res) => {
    let { ids } = req.body
    let obj = {}
    let sql = `delete from address where id in (?)`
    mysql.query(sql, [ids], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '删除成功'
        obj.data = result
        console.log('删除成功');
        res.send(obj)
    })
})


// 修改地址信息
router.post('/update_address', (req, res) => {
    let { id, user_id, consignee, sex, phone, address_book, detail } = req.body
    let obj = {}
    let sql = `update address set user_id=?,consignee=?,sex=?,phone=?,address_book=?,detail=? where id=?`
    mysql.query(sql, [user_id, consignee, sex, phone, address_book, detail, id], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '修改成功'
        obj.data = result
        console.log('修改成功');
        res.send(obj)
    })
})


// 查询购物车信息
router.get('/inquire_shop', (req, res) => {
    let obj = {}
    let sql = `select s.*,d.name,d.price from shopping_cart s left join dish d on s.dish_id = d.id;`
    mysql.query(sql, (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '查询成功'
        obj.data = result
        console.log('查询成功');
        res.send(obj)
    })
})

// 点击结算时传入ID值，修改表是否选中
router.post('/inquire_shop_selected', (req, res) => {
    let{ids} = req.body
    let obj = {}
    console.log("ids",ids);
    let sql = `update shopping_cart set selected=? where dish_id in (?)`
    mysql.query(sql, [1,ids],(err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '查询成功'
        obj.data = result
        console.log('修改成功');
        res.send(obj)
    })
})
// 重新加载购物车页时修改所有购物车中的商品为未选中
router.post('/inquire_shop_unselected', (req, res) => {
    let{ids} = req.body
    let obj = {}
    console.log("ids",ids);
    let sql = `update shopping_cart set selected=?`
    mysql.query(sql, 0,(err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '查询成功'
        obj.data = result
        console.log('修改成功');
        res.send(obj)
    })
})

// 查询购物车选中信息
router.get('/inquire_shop_order', (req, res) => {
    let obj = {}
    let sql = `select s.*,d.name,d.price from shopping_cart s left join dish d on s.dish_id = d.id where s.selected = 1;`
    mysql.query(sql,(err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '查询成功'
        obj.data = result
        console.log('查询成功');
        res.send(obj)
    })
})


// 添加购物车信息
router.post('/add_shop', (req, res) => {
    let { dishid, number } = req.body

    let obj = {}
    let sql = `select * from dish where id=?`
    mysql.query(sql, [dishid], (err, result) => {
        if (err) {
            return console.log(err);
        }
        let image = result[0].image
        let dish_id = result[0].id
        let user_id = 1
        let sql1 = `select * from shopping_cart where shopping_cart.dish_id = ?`
        mysql.query(sql1,[dish_id], (err, result) => {
            console.log(result.length);
            if (result.length > 0) {
                number = parseInt(result[0].number ) +parseInt(number)  
                let sql3 = `update shopping_cart set number=? where dish_id=?`
                mysql.query(sql3, [number, dish_id], (err, result) => {
                    if (err) {
                        return console.log(err);
                    }
                    obj.code = 200
                    obj.desc = '添加成功'
                    obj.data = result
                    console.log('添加成功');
                    res.send(obj)
                })
            } else {
                let sql2 = `insert into shopping_cart (image,user_id,dish_id,number) value (?,?,?,?)`
                mysql.query(sql2, [image, user_id, dish_id, number], (err, result) => {
                    if (err) {
                        return console.log(err);
                    }
                    obj.code = 201
                    obj.desc = '添加成功'
                    obj.data = result
                    console.log('添加成功');
                    res.send(obj)
                })
            }
        })


    })


})
// 删除购物车信息
router.post('/delete_shop', (req, res) => {
    let { ids } = req.body
    let obj = {}
    console.log(ids);
    let sql = `delete from shopping_cart where dish_id in (?)`
    mysql.query(sql, [ids], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '删除成功'
        obj.data = result
        console.log('删除成功');
        res.send(obj)
    })
})


// 修改购物车添加信息
router.post('/update_shop_add', (req, res) => {
    let { id, number } = req.body
    let obj = {}
    let sql = `update shopping_cart set number=? where id=?`
    mysql.query(sql, [number, id], (err, result) => {
        if (err) {
            return console.log(err);
        }
        obj.code = 200
        obj.desc = '修改成功'
        obj.data = result
        console.log('修改成功');
        res.send(obj)
    })
})
// 修改购物车减少信息
router.post('/update_shop_delete', (req, res) => {
    let { id, number } = req.body
    let obj = {}
    number = number - 1
    if (number <= 0) {
        let sql = `delete from shopping_cart where id =?`
        mysql.query(sql, [id], (err, result) => {
            if (err) {
                return console.log(err);
            }
            obj.code = 200
            obj.desc = '删除成功'
            obj.data = result
            console.log('删除成功');
            res.send(obj)
        })
    } else {
        let sql = `update shopping_cart set number=? where id=?`
        mysql.query(sql, [number, id], (err, result) => {
            if (err) {
                return console.log(err);
            }
            obj.code = 200
            obj.desc = '修改成功'
            obj.data = result
            console.log('修改成功');
            res.send(obj)
        })
    }


})
// 前台登陆
router.post('/login_q', (req, res) => {
    let {username,password} = req.body
    console.log(req.body);
    let obj={}
    let sql =`select * from user where name=? and password=?`
    mysql.query(sql,[username,password],(err,result)=>{
        console.log(result);
        if(err){
            return  console.log(err);
        }
        if(result.length >0){
            obj.code=200
          
            obj.desc='登陆成功'
        }else{
            obj.code=404
            
            obj.desc='登陆失败'
        }
       
        obj.data=result
        console.log(obj);
        res.send(obj)
    })
})

// 前台注册
router.post('/register_q', (req, res) => {
    let {username,password,nickname} = req.body
    console.log(req.body);
    let obj={}
    let sql =`insert into user (name,password,nickname) value (?,?,?)`
    mysql.query(sql,[username,password,nickname],(err,result)=>{
        console.log(result);
        if(err){
            return  console.log(err);
        }
            obj.code=200
            obj.desc='注册成功'
        obj.data=result
        console.log(obj);
        res.send(obj)
    })
})
// 查询用户信息
router.get('/usercase', (req, res) => {
    let{id} = req.query
    let obj={}
    console.log(req.query);
    let sql =`select * from user where id = ?`
    mysql.query(sql,[id],(err,result)=>{
        if(err){
            return  console.log(err);
        }
        obj.code=200
        obj.desc='查询成功'
        obj.data=result
        console.log('查询成功');
        res.send(obj)
    })
})


module.exports = router;